package com.zhouzhou.fastjava.criteria;

import com.zhouzhou.fastjava.provider.UserIdProvider;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class SpringMVCRequestHeaderUserIdProvider implements UserIdProvider {

    private final String key;

    public SpringMVCRequestHeaderUserIdProvider(String key) {
        this.key = key;
    }

    public HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    }

    @Override
    public String getUserId() {
        return getRequest().getHeader(key);
    }

}
